//
//  PopoverAnimationManager.swift
//  WeiBo
//
//  Created by Gao on 16/3/6.
//  Copyright © 2016年 Gao. All rights reserved.
//

import UIKit
//改变标题栏箭头方向的通知
let AnimationWillShowOrWillDimiss = "AnimationWillShowOrWillDimiss"
class PopoverAnimationManager: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning
{
    /**转场动画是否被展示*/
    var isPresent:Bool = false
    
    /**转场动画的位置和大小*/
    var presentFrame:CGRect = CGRectZero
    
    //UIPresentationController属于过渡控制器 连接发起展示的控制和 被展示的控制器
    
    //返回负责转场的控制器对象
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?
    {
        let pop = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        if presentFrame == CGRectZero
        {
            presentFrame = CGRect(x: 100, y: 56, width: 200, height: 200)
        }
        pop.presentFrame = presentFrame
        return  pop
    }
    
    //转场动画的开始 谁负责
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresent = true
        NSNotificationCenter.defaultCenter().postNotificationName(AnimationWillShowOrWillDimiss, object: nil)
        return self
    }
    
    //转场动画的结束 谁负责
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresent = false
        NSNotificationCenter.defaultCenter().postNotificationName(AnimationWillShowOrWillDimiss, object: nil)
        return self
    }
    
    //MARK: - UIViewControllerAnimatedTransitioning
    //实现这个代理方法 意味着系统什么都不给你提供了 包括默认动画等等
    //动画的时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
    {
        return 0.5
    }
    //动画的设置
    func animateTransition(transitionContext: UIViewControllerContextTransitioning)
    {
        //fromView  和  toView指的是同一个View
        if isPresent
        {
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            
            //因为系统不会默认添加这个页面  所以需要手动添加
            transitionContext.containerView()?.addSubview(toView)
            
            toView.transform = CGAffineTransformMakeScale(1.0, 0.0)
            
            //矛点 设置动画出现的位置
            toView.layer.anchorPoint = CGPoint(x: 0.0, y: 0.0)
            //  transitionDuration(transitionContext)保持转场动画时间的一致性
            UIView.animateWithDuration(transitionDuration(transitionContext), animations:
                {
                    () -> Void in
                    //还原transform
                    toView.transform = CGAffineTransformIdentity
                    
                }, completion: { (_) -> Void in
                    // 完成之后一定要调用这个方法 这边不写 弹出来的界面是不能交互的
                    transitionContext.completeTransition(true)
            })
        }
        else
        {
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
            UIView.animateWithDuration(transitionDuration(transitionContext), animations:
                {
                    () -> Void in
                    fromView.transform = CGAffineTransformMakeScale(1.0, 0.00001)
                }, completion:
                {
                    (_) -> Void in
                    fromView.removeFromSuperview()
                    // 完成之后一定要调用这个方法 这边不写 弹出来的界面 回不去
                    transitionContext.completeTransition(true)
            })
        }
    }
}

